package com.jushu.storbox.view.resolve;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.ActionLinkActivity;
import com.jushu.storbox.ActionShareActivity;
import com.jushu.storbox.R;
import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.actions.ActionShare;
import com.jushu.storbox.actions.AuthorizationAction;
import com.jushu.storbox.actions.DownloadAction;
import com.jushu.storbox.actions.FileAction;
import com.jushu.storbox.actions.FolderAction;
import com.jushu.storbox.actions.NodeAction;
import com.jushu.storbox.adapter.FileDisplayAdapter;
import com.jushu.storbox.callback.FileFolderInfoFileSizeSort;
import com.jushu.storbox.callback.FileFolderInfoNameSort;
import com.jushu.storbox.callback.FileFolderInfoTimeSort;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.IntentConstant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.constant.UiConstant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FileType;
import com.jushu.storbox.handler.ClientExceptionRelateHandler;
import com.jushu.storbox.newfragment.argument.FileActionArgs;
import com.jushu.storbox.operation.group.FileItemMoreOperation;
import com.jushu.storbox.operation.group.FileOperation;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.view.dialog.AbsPopupMenuDialog;
import com.jushu.storbox.view.dialog.DeleteFileDialog;
import com.jushu.storbox.view.dialog.DownloadDialogWithCheckWifi;
import com.jushu.storbox.view.dialog.PopupMenuWindow;
import com.jushu.storbox.view.dialog.RenameDialog;
import com.jushu.storbox.vo.PagedList;
import com.huawei.sharedrive.sdk.android.exception.ClientException;
import com.huawei.sharedrive.sdk.android.util.StringUtil;
import com.kennyc.view.MultiStateView;

public class FilleDisplayResolve
		extends ListDisplayResolve<FileFolderInfo, FileItemMoreOperation<FileFolderInfo>, FileDisplayAdapter>
		implements FileOperation<FileFolderInfo> {
	protected final static String TAG = FilleDisplayResolve.class.getSimpleName();

	private FileActionArgs folderArgument = null;
	private Stack<FileActionArgs> folderStack = new Stack<FileActionArgs>();
	private List<FileFolderInfo> datas = new ArrayList<FileFolderInfo>();
	private ProgressDialog waitDialog;
	private String clickedItemOwnerId = null;
	private MultiStateView multiStateView;

	FileItemMoreOperationImpl operation = new FileItemMoreOperationImpl();

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			multiStateView.setViewState(MultiStateView.ViewState.LOADING);
			loadDataList();
		}
	};

	public FilleDisplayResolve(Activity activity, FileActionArgs folderArgument, MultiStateView multiStateView) {
		super(activity);
		this.folderArgument = folderArgument;
		this.multiStateView = multiStateView;
		multiStateView.getView(MultiStateView.ViewState.ERROR).findViewById(R.id.button1).setOnClickListener(listener);
	}

	public FilleDisplayResolve(Activity activity, FileActionArgs folderArgument) {
		super(activity);
		this.folderArgument = folderArgument;
	}

	// init method
	@Override
	public void initComponent(PullToRefreshListView pullToRefreshListView) {
		super.initComponent(pullToRefreshListView);
		waitDialog = new ProgressDialog(context);
		waitDialog.setMessage(context.getResources().getString(R.string.loading));
	}

	@Override
	protected FileDisplayAdapter initTemplateAdapter() {
		adapter = new FileDisplayAdapter(getActivity(), datas);
		adapter.setPopWindow(operation.popWindow);
		return adapter;
	}

	@Override
	protected FileItemMoreOperation<FileFolderInfo> initAdapterOperation() {
		return operation;
	}

	// setting and getting
	private Activity getActivity() {
		return (Activity) context;
	}

	public Stack<FileActionArgs> getFolderStack() {
		return folderStack;
	}

	public FileActionArgs getFolderArgument() {
		return folderArgument;
	}

	@Override
	protected void clickItem(View clickView, int position, long id, FileFolderInfo info) {
		clickedItemOwnerId = info.getOwnerId();
		if (FileType.File.value() == info.getIsFile()) {
			openFile(position, info);
		} else {
			openFolder(position, info);
		}
	}

	private void openFile(int index, FileFolderInfo info) {
		new FileAction().openFile(context, info, msgHandler);
	}

	protected void openFolder(int index, FileFolderInfo info) {
		new FolderAction().openFolder(context, MessageCode.OPEN_FOLDER, info.getOwnerId(), info.getId(), info.getName(),
				Constant.FIRST_PAGE_INDEX, msgHandler);
	}

	public void openFolderWithWaiting(int index, FileFolderInfo info) {
		waitDialog.show();
		new FolderAction().openFolder(context, MessageCode.OPEN_FOLDER, info.getOwnerId(), info.getId(), info.getName(),
				Constant.FIRST_PAGE_INDEX, msgHandler);
	}

	private void doStartDownloadTask(FileFolderInfo info) {
		if (info.getIsFile() == FileType.File.value()) {
			new DownloadAction().downloadFile(context, info, true, msgHandler);
		} else if (info.getIsFile() == FileType.Folder.value()) {
			LogUtil.d(TAG, "download node is folder!");
			new DownloadAction().downloadFolder(context, info, true, msgHandler);
		} else {

		}
	}

	public void openParentFolder() {
		if (folderStack.size() > 1) {
			folderStack.pop(); // 栈顶元素是当前正在显示的目录（包括根目录）
			FileActionArgs parentArgument = folderStack.peek();
			clickedItemOwnerId = getOpendResourceOwnerId();
			if (StringUtil.isNotBlank(clickedItemOwnerId)) {
				new FolderAction().openFolder(context, MessageCode.OPEN_PARENT_FOLDER, clickedItemOwnerId,
						parentArgument.getFolderId(), parentArgument.getFolderName(), Constant.FIRST_PAGE_INDEX,
						msgHandler);
			} else {
				LogUtil.e(TAG, "ownerId is null");
			}
		} else {
			LogUtil.d(TAG, "no more parent folder!");
		}
	}

	public void getMoreDataList() {
		if (folderArgument.getKeyWords() == null) {
			clickedItemOwnerId = getOpendResourceOwnerId();
			if (StringUtil.isNotBlank(clickedItemOwnerId)) {
				new FolderAction().getMoreNodeListFromServer(context, clickedItemOwnerId, folderArgument.getFolderId(),
						folderArgument.getPageIndex(), msgHandler);
			} else {
				LogUtil.e(TAG, "ownerId is null");
			}
		} else if (StringUtil.isNotBlank(folderArgument.getKeyWords())) {
			new FileAction().searchFileListFromServer(folderArgument.getOwnerId(), folderArgument.getKeyWords(),
					folderArgument.getPageIndex(), getActivity(), msgHandler);
		}
	}

	@Override
	protected void refreshDataList() {
		if (folderArgument.getKeyWords() == null) {
			folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
			clickedItemOwnerId = getOpendResourceOwnerId();
			if (StringUtil.isNotBlank(clickedItemOwnerId)) {
				new FolderAction().openFolder(context, MessageCode.LOAD_FILE_LIST, clickedItemOwnerId,
						folderArgument.getFolderId(), folderArgument.getFolderName(), Constant.FIRST_PAGE_INDEX,
						msgHandler);
			} else {
				LogUtil.e(TAG, "ownerId is null");
			}
		} else if (StringUtil.isNotBlank(folderArgument.getKeyWords())) {
			folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
			new FileAction().searchFileListFromServer(folderArgument.getOwnerId(), folderArgument.getKeyWords(),
					folderArgument.getPageIndex(), getActivity(), msgHandler);
		}
	}

	protected void loadDataList() {
		if (folderArgument.getKeyWords() == null) {
			folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
			clickedItemOwnerId = getOpendResourceOwnerId();
			if (StringUtil.isNotBlank(clickedItemOwnerId)) {
				new FolderAction().openFolder(context, MessageCode.OPEN_FOLDER, clickedItemOwnerId,
						folderArgument.getFolderId(), folderArgument.getFolderName(), Constant.FIRST_PAGE_INDEX,
						msgHandler);
			} else {
				LogUtil.e(TAG, "ownerId is null");
			}
		} else if (StringUtil.isNotBlank(folderArgument.getKeyWords())) {
			folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
			new FileAction().searchFileListFromServer(folderArgument.getOwnerId(), folderArgument.getKeyWords(),
					folderArgument.getPageIndex(), getActivity(), msgHandler);
		}
	}

	protected final String aqureOpendResourceOwnerId() {
		return ShareDriveApplication.getInstance().getWnerID();
	}

	private String getOpendResourceOwnerId() {
		if (StringUtil.isBlank(clickedItemOwnerId)) {
			clickedItemOwnerId = aqureOpendResourceOwnerId();
		}
		return clickedItemOwnerId;
	}

	@Override
	public void binddingAdapter() {
		clickedItemOwnerId = null;
		super.binddingAdapter();
	}

	protected void updateCurrentFolder(String ownerId, String folderId, String folderName) {
	}

	ClientExceptionRelateHandler msgHandler = new ClientExceptionRelateHandler() {

		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			Log.i("test", "msg.what = " + msg.what);
			
			if (msg.obj instanceof ClientException && null != multiStateView) {
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			} else if (null != multiStateView){
				multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
			}

			switch (msg.what) {
			case MessageCode.LOAD_FILE_LIST:
				handlerLoadFileList(msg);
				waitDialog.dismiss();
				break;
			case MessageCode.OPEN_FOLDER:
				PagedList<FileFolderInfo> res = (PagedList<FileFolderInfo>) msg.obj;
				if (waitDialog.isShowing()) {
					waitDialog.dismiss();
				}
				if (folderStack.isEmpty()) {
					folderStack.push(folderArgument); // 根目录入栈
				} else {
					folderArgument = new FileActionArgs();
					folderArgument.setFolderId(res.getPagedFolderId());
					folderArgument.setFolderName(res.getPagedFolderName());
					folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
					folderStack.push(folderArgument); // 当前显示的目录入栈
				}
				updateCurrentFolder(null, folderArgument.getFolderId(), folderArgument.getFolderName());
				handlerLoadFileList(msg);
				break;
			case MessageCode.OPEN_PARENT_FOLDER:
				if (null != multiStateView && multiStateView.getViewState() != MultiStateView.ViewState.CONTENT) {
					multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
				}
				PagedList<FileFolderInfo> tmpRes = (PagedList<FileFolderInfo>) msg.obj;
				folderArgument.setFolderId(tmpRes.getPagedFolderId());
				folderArgument.setFolderName(tmpRes.getPagedFolderName());
				folderArgument.setPageIndex(Constant.FIRST_PAGE_INDEX);
				updateCurrentFolder(null, folderArgument.getFolderId(), folderArgument.getFolderName());
				clickedItemOwnerId = getOpendResourceOwnerId();
				handlerLoadFileList(msg);
				break;
			case MessageCode.DELETE_FILE_SUCCESS:
				waitDialog.dismiss();
				adapter.removeData((FileFolderInfo) msg.obj);
				break;
			case UiConstant.NODE_SEARCH_BY_KEYWORD:
				handlerLoadFileList(msg);
				waitDialog.dismiss();
				break;
			case MessageCode.TRANSFER_TO_MY_TEAM_SUCCESS:
				showToast(R.string.action_transfer_success, Toast.LENGTH_SHORT);
				break;
			case MessageCode.RENAME_SUCCESS:
				waitDialog.dismiss();
				adapter.notifyDataSetChanged();
				break;
			case UiConstant.NODE_ACTION_SHARE_TEAMSPACE_SUCCESS:
				List<FileFolderInfo> errorShare = (List<FileFolderInfo>) msg.obj;
				if (errorShare.isEmpty()) {
					showToast(R.string.action_transfer_success, Toast.LENGTH_SHORT);
				} else {
					// 共转存到团队空间%1$d个,失败%2$d个
					String format = context.getResources().getString(R.string.action_transfer_some_error);
					String result = String.format(format, msg.arg1, msg.arg2);
					showToast(result, Toast.LENGTH_SHORT);
				}
				break;
			default:
				if (waitDialog.isShowing()) {
					waitDialog.dismiss();
				}
				super.handleMessage(msg);
			}
		}

		@SuppressWarnings("unchecked")
		private void handlerLoadFileList(Message msg) {
			PagedList<FileFolderInfo> pagedList = (PagedList<FileFolderInfo>) msg.obj;
			List<FileFolderInfo> pagedData = pagedList.getPagedList();
			onRefreshComplete();
			if (folderArgument.getPageIndex() == Constant.FIRST_PAGE_INDEX) {
				adapter.cleanListDatasOnly();
			}
			if (pagedData == null) {
				return;
			}
			adapter.addDatas(pagedData);
			folderArgument.incPageIndex();
			// unlock screen
		}

		@Override
		protected void onHandlerBefore(Message msg, String errorCode) {
			onRefreshComplete();
			super.onHandlerBefore(msg, errorCode);
		}

		@Override
		public Context getContext() {
			return getActivity();
		}
	};

	class FileItemMoreOperationImpl implements FileItemMoreOperation<FileFolderInfo> {
		public PopupMenuWindow popWindow;
		AbsPopupMenuDialog popMenu;
		View executeDownload;
		View cancelDownload;
		View syncDownload;

		int mIndex;
		FileFolderInfo mInfo;

		public FileItemMoreOperationImpl() {
			initPopWindow();
			initPopMenu();
		}

		private void initPopMenu() {
			popMenu = new AbsPopupMenuDialog(context, R.style.dialog_upload, R.layout.adapter_view_allfile_more_item) {
				public void onInflaterFinished(View contentView) {
					View teamAction = contentView.findViewById(R.id.adapter_view_allfile_more_menu_team);
					teamAction.setOnClickListener(new View.OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "share team: " + mIndex);
							dismiss();
							onTransferToTeam(mIndex, mInfo);
						}
					});
					View moveAction = contentView.findViewById(R.id.adapter_view_allfile_more_menu_move);
					moveAction.setOnClickListener(new View.OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "move: " + mIndex);
							dismiss();
							onMove(mIndex, mInfo);
						}
					});
					View deleteAction = contentView.findViewById(R.id.adapter_view_allfile_more_menu_delete);
					deleteAction.setOnClickListener(new View.OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "delete: " + mIndex);
							dismiss();
							onDelete(mIndex, mInfo);
						}
					});
					View renameAction = contentView.findViewById(R.id.adapter_view_allfile_more_menu_rename);
					renameAction.setOnClickListener(new View.OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "rename: " + mIndex);
							dismiss();
							onRename(mIndex, mInfo);
						}
					});
				}
			};

			WindowManager windowManager = getActivity().getWindowManager();
			Display display = windowManager.getDefaultDisplay();
			popMenu.setWindowWidth(display.getWidth());
			// popMenu.setWindowWidth((int)
			// context.getResources().getDimension(R.dimen.item_popup_more_window_width_ll));
			popMenu.setWindowPosition(Gravity.CENTER, 0, 0);

		}

		private void initPopWindow() {
			popWindow = new PopupMenuWindow(context, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
					R.layout.adapter_view_item_window_more) {

				@Override
				protected void onInflaterFinished(View contentView) {
					executeDownload = contentView.findViewById(R.id.adapter_view_item_window_more_download_execute);
					cancelDownload = contentView.findViewById(R.id.adapter_view_item_window_more_download_cancel);
					syncDownload = contentView.findViewById(R.id.adapter_view_item_window_more_download_sync);
					View shareAction = contentView.findViewById(R.id.adapter_view_item_window_more_share);
					shareAction.setOnClickListener(new OnClickListener() {
						public void onClick(View v) {
							LogUtil.i(TAG, "share: " + mIndex);
							dismiss();
							if (mInfo != null) {
								onShareItem(mIndex, mInfo);
							} else {
								LogUtil.e(TAG, "the data is null");
							}
						}
					});
					View linkAction = contentView.findViewById(R.id.adapter_view_item_window_more_link);
					linkAction.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "link: " + mIndex);
							dismiss();
							if (mInfo != null) {
								onLinkItem(mIndex, mInfo);
							} else {
								LogUtil.e(TAG, "the data is null");
							}
						}
					});
					executeDownload.setOnClickListener(new OnClickListener() {
						public void onClick(View arg0) {
							LogUtil.i(TAG, "download: " + mIndex);
							dismiss();
							if (mInfo != null) {
								onDownloadItem(mIndex, mInfo);
							} else {
								LogUtil.e(TAG, "the data is null");
							}
						}
					});
					cancelDownload.setOnClickListener(new OnClickListener() {
						public void onClick(View paramView) {
							dismiss();
							if (mInfo != null) {
								new NodeAction().cleanNodeCatch(getActivity(), msgHandler, mInfo);
							} else {
								LogUtil.e(TAG, "the data is null");
							}
						}
					});
					syncDownload.setOnClickListener(new OnClickListener() {
						public void onClick(View paramView) {
							dismiss();
							if (mInfo != null) {
								new NodeAction().syncNode(getActivity(), msgHandler, mInfo);
							} else {
								LogUtil.e(TAG, "the data is null");
							}
						}
					});
					View moreAction = contentView.findViewById(R.id.adapter_view_item_window_more_moreaction);
					moreAction.setOnClickListener(new OnClickListener() {
						public void onClick(View view) {
							dismiss();
							popMenu.show();
						}
					});
				}
			};
		}

		@Override
		public void onItemMore(final int index, final FileFolderInfo Info) {
			this.mIndex = index;
			this.mInfo = Info;

			if (popWindow == null) {
				initPopWindow();
			}

			if (popMenu == null) {
				initPopMenu();
			}
			// set the view to default statues
			executeDownload.setVisibility(View.GONE);
			cancelDownload.setVisibility(View.GONE);
			syncDownload.setVisibility(View.GONE);

			// decade witch view to see
			switch (Info.getTransStatus()) {
			case Constant.DOWNLOAD_NON_UPDATE_DB:
				executeDownload.setVisibility(View.VISIBLE);
				break;
			case Constant.DOWNLOAD_ISRUNNING_UPDATE_DB:
				cancelDownload.setVisibility(View.VISIBLE);
				break;
			case Constant.DOWNLOAD_ISWAITING_UPDATE_DB:
				cancelDownload.setVisibility(View.VISIBLE);
				break;
			case Constant.DOWNLOAD_CANCEL_UPDATE_DB:
				executeDownload.setVisibility(View.VISIBLE);
				break;
			case Constant.DOWNLOAD_FAIL_UPDATE_DB:
				syncDownload.setVisibility(View.VISIBLE);
				break;
			case Constant.DOWNLOAD_SUCCESS_UPDATE_DB:
				switch (Info.getContentSyncState()) {
				case Constant.SYNC_STATUS_TRUE:
					cancelDownload.setVisibility(View.VISIBLE);
					break;
				case Constant.SYNC_STATUS_FALSE:
					syncDownload.setVisibility(View.VISIBLE);
					break;
				}
				break;
			}
		}
	}

	public void onShareItem(int index, FileFolderInfo info) {
		if (new AuthorizationAction(getActivity()).checkIsRootAndNotify(getActivity(), msgHandler)) {
			return;
		}
		Intent intent = new Intent(getActivity(), ActionShareActivity.class);
		intent.putExtra(IntentConstant.INTENT_SHARE_DATA, info);
		getActivity().startActivity(intent);
	}

	public void onLinkItem(int index, FileFolderInfo info) {
		Intent linkIntent = new Intent();
		linkIntent.setClass(getActivity(), ActionLinkActivity.class);
		linkIntent.putExtra(IntentConstant.INTENT_LINK_DATA, info);
		getActivity().startActivityForResult(linkIntent, IntentConstant.INTENT_REQUEST_LINK);
	}

	public void onDownloadItem(int index, final FileFolderInfo info) {
		new DownloadDialogWithCheckWifi() {
			@Override
			protected void executeDownloadTask(FileFolderInfo info) {
				doStartDownloadTask(info);
				super.executeDownloadTask(info);
			}
		}.download(getActivity(), info);
	}
	
	@Override
	public void addData(int index, FileFolderInfo data) {
		// TODO Auto-generated method stub
		super.addData(index, data);
		if (null != multiStateView &&multiStateView.getViewState() != MultiStateView.ViewState.CONTENT) {
			multiStateView.setViewState(MultiStateView.ViewState.CONTENT);
		}
	}

	public void onShareTeam(int index, FileFolderInfo info) {
	}

	public void onMove(int index, FileFolderInfo info) {
	}

	public void onTransferToTeam(int index, FileFolderInfo info) {
	}

	public void doTransferToTeam(Bundle bundle) {
		int sourceType = bundle.getInt(IntentConstant.INTENT_SOURCE_RESOURCE_TYPE);
		String targetResourceID = bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_ID);
		String targetResourceOwnerID = bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER);
		FileFolderInfo info = (FileFolderInfo) bundle.getSerializable(IntentConstant.INTENT_DATA_EXTENDS_1);
		if (StringUtil.isNotBlank(targetResourceID) && StringUtil.isNotBlank(targetResourceOwnerID)) {
			if (sourceType == FileType.File.value()) {
				new ActionShare().copyShareFile(getActivity(), MessageCode.TRANSFER_TO_MY_TEAM_SUCCESS,
						info.getOwnerId(), info.getId(), targetResourceOwnerID, targetResourceID, msgHandler);
			} else if (sourceType == FileType.Folder.value()) {
				new ActionShare().copyShareFolder(getActivity(), MessageCode.TRANSFER_TO_MY_TEAM_SUCCESS,
						info.getOwnerId(), info.getId(), targetResourceOwnerID, targetResourceID, msgHandler);
			}
		} else {
			LogUtil.e(TAG, "some fileds is empty!");
		}
	}

	public void doTransferToTeam(Bundle bundle, List<FileFolderInfo> datas) {
		String targetResourceID = bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_ID);
		String targetResourceOwnerID = bundle.getString(IntentConstant.INTENT_TARGET_RESOURCE_OWNER);
		if (StringUtil.isNotBlank(targetResourceID) && StringUtil.isNotBlank(targetResourceOwnerID)) {
			new NodeAction().saveToTeamSpace(context, msgHandler, UiConstant.NODE_ACTION_SHARE_TEAMSPACE_SUCCESS, datas,
					targetResourceID, targetResourceOwnerID);
		} else {
			LogUtil.e(TAG, "some fileds is empty!");
		}
	}

	public void onDelete(int index, final FileFolderInfo info) {
		new DeleteFileDialog().delete(getActivity(), ShareDriveApplication.getInstance().getWnerID(), info, msgHandler,
				waitDialog);

	}

	public void onRename(int index, FileFolderInfo info) {
		new RenameDialog().rename(getActivity(), info, waitDialog, msgHandler);
	}

	public void sortByName(int sortType) {
		adapter.sort(new FileFolderInfoNameSort(sortType));
		writePreferenceForSort(UiConstant.FILE_SORT_NAME, sortType);
	}

	public void sortByTime(int sortType) {
		adapter.sort(new FileFolderInfoTimeSort(sortType));
		writePreferenceForSort(UiConstant.FILE_SORT_TIME, sortType);
	}

	public void sortBySize(int sortType) {
		adapter.sort(new FileFolderInfoFileSizeSort(sortType));
		writePreferenceForSort(UiConstant.FILE_SORT_SIZE, sortType);
	}

	void writePreferenceForSort(String str, int sortType) {
		SharedPreferences preference = context.getSharedPreferences(ClientConfig.settings, Context.MODE_APPEND);
		Editor edit = preference.edit();
		edit.putInt(str, sortType);
		edit.putInt(UiConstant.FILE_SORT_CURRENT, sortType);
		edit.commit();
	}

}
